/*
 * ZENworks Notification Icon
 * Copyright (C) Author: Gangadhar Nagesh Metla (Novell, Inc.)
 * nmetla74@gmail.com 
 * Version 1.0
 * 1/3/2009
 * Filename ZUIServer.java
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.novell.zenworks.agent.zicon.remoting;

import java.awt.Frame;
import java.rmi.RemoteException;

import javax.swing.JFrame;

import com.novell.zenworks.agent.common.ICommonUIEnums.MB_ICON;
import com.novell.zenworks.agent.zbrowser.ZBrowser;
import com.novell.zenworks.agent.zbrowser.cmddialogs.BundlePropDialog;
import com.novell.zenworks.agent.zicon.PageApp;
import com.novell.zenworks.agent.zicon.ZTrayIcon;
import com.novell.zenworks.agent.zicon.progress.ZActivityController;
import com.novell.zenworks.agent.zicon.progress.ZProgressDialog;
import com.novell.zenworks.agent.zicon.progress.ZProgressDialogLauncher;
import com.novell.zenworks.ipc.remoting.ZenRemoteObject;
import com.novell.zenworks.zicon.common.IZenIconUIServer;
import com.novell.zenworks.zicon.common.dialogs.IBundlePropData;
import com.novell.zenworks.zicon.common.progress.ITaskSetProgress;
import com.novell.zenworks.zicon.common.progress.ITaskSetProgress.ProgressStatus;


public class ZUIServer extends ZenRemoteObject implements IZenIconUIServer {

	private static final long serialVersionUID = 1L;	
	private static ZUIServer _instance = null;
	private static Object lock = new Object(); //for synchronization
	private ZTrayIcon trayicon;

	private ZUIServer(ZTrayIcon trayicon) throws RemoteException
    {
       this.trayicon = trayicon;
    }
	
    public static ZUIServer getInstance() 
	{
		return _instance;
	}
	
	public static ZUIServer createUIServerInstance(ZTrayIcon trayIcon)
	{
	    if(_instance == null)
        {
            synchronized(lock)
            {
                if(_instance == null)
                {
                    try {
						_instance = new ZUIServer(trayIcon);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                }
            }
        }
	   return _instance;	    
	}
	
    public void setVisible(boolean visible) throws RemoteException
    {
        trayicon.setVisible(visible);        
    }
    
    public void startAnimation() throws RemoteException
    {
        trayicon.startAnimation();
    }
    
    public void stopAnimation() throws RemoteException
    {
        trayicon.stopAnimation();    
    }
    
    public void showBalloonTip(String message, String title, MB_ICON iconType, int timeout) throws RemoteException
    {
       trayicon.showBalloonTip(message, title, iconType, timeout) ;        
    }
    
    public void showDataFormDialog(String pageId, int timeout) throws RemoteException
    {
       try
       {
           PageApp.launchDataform(pageId, timeout);
       } 
       catch (Exception e)
       {
           throw new RemoteException(e.getMessage());
       }   
    }

    public void showProgress() throws RemoteException
    {
        try
        {
            Thread t = new Thread(new ZProgressDialogLauncher());
            t.start();
        } catch (Exception e)
        {
            throw new RemoteException(e.getMessage());
        }
        
    }

    public void addProgress(ITaskSetProgress progress) throws RemoteException
    {
        try
        {
            ZProgressDialog.getInstance().addProgressPanel(progress);
        } catch (Exception e)
        {
            throw new RemoteException(e.getMessage());
        }
        
    }

    public void enableShowProgress(boolean enable) throws RemoteException
    {
        try
        {
            Thread t = new Thread(new ZProgressDialogLauncher());
            t.start();
        } catch (Exception e)
        {
            throw new RemoteException(e.getMessage());
        }
        
    }    

    public boolean showActivityIndicator(String guid, ProgressStatus status, String name) throws RemoteException
    {
        return ZActivityController.getInstance().launchActivityDialog(guid, status, name);
    }

    public boolean updateActivityIndicator(String guid, ProgressStatus action, ProgressStatus status, String name) throws RemoteException
    {
        return ZActivityController.getInstance().updateActivityData(guid, action, status, name);
    }
    
    public boolean closeActivityIndicator(String guid) throws RemoteException
    {
        return ZActivityController.getInstance().disposeActivityDialog(guid);
    }

	public void showBundlePropertiesDialog(IBundlePropData data) throws RemoteException 
	{
		if(PageApp.browser != null)
            ZBrowser.launchBundlePropertiesDialog((JFrame)ZBrowser.getFrames()[0], data);
		else
		{
			ZBrowser.launchBundlePropertiesDialog(new JFrame(), data);
		}
	}
	
	public void launchFileInBrowser(String path) throws RemoteException
	{
		ZTrayIcon.launchHelpFile(path);
	}

	public void launchFileInEditor(String editor, String filePath) throws RemoteException {
		ZTrayIcon.launchTextFile(editor, filePath);
	}

	public void refreshPage() throws RemoteException {
		try {
			PageApp.refreshPage();
		} catch (Exception e) {
			 throw new RemoteException(e.getMessage());
		}
		
	}
	
	public void setDefaultPage() throws RemoteException
    {
		try {
			PageApp.setDefaultPage();
		} catch (Exception e) {
			 throw new RemoteException(e.getMessage());
		}
    }

}
